﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace News.CYT369.Com.Domains.WebRequest
{
    public class HttpParameter {
        private System.IO.Stream m_Stream = null;
        private object m_Object = null;
        private string m_Value = null;

        public string Key { get; set; }
        public string FileName { get; set; }

        public bool IsStream {
            get { return m_Stream != null; }
        }

        public bool IsObject {
            get {
                return this.m_Object != null;
            }
        }

        public bool IsJsonString
        {
            get;set;
        }

        public string Value {
            get { return m_Value; }
            set {
                if (m_Stream != null) { throw new ArgumentException("cannot set Value when Stream is not null"); }
                if (m_Object != null) { throw new ArgumentException("cannot set Value when Object is not null"); }
                m_Value = value;
            }
        }

        public System.IO.Stream Stream {
            get { return m_Stream; }
            set {
                if (m_Value != null) { throw new ArgumentException("cannot set Stream when Value is not null"); }
                m_Stream = value;
            }
        }

        public Object @Object {
            get {
                return this.m_Object;
            }
            set {
                if (this.m_Value != null)
                {
                    throw new ArgumentException("cannot set Object when Value is not null");
                }
                this.m_Object = value;
            }
        }

        public HttpParameter(string key, string value) {
            if (string.IsNullOrEmpty(key)) { throw new ArgumentNullException("key"); }
            this.Key = key;
            this.Value = value;
        }

        public HttpParameter(string key, string fileName, System.IO.Stream stream) {
            if (string.IsNullOrEmpty(fileName)) { throw new ArgumentNullException("fileName"); }
            this.FileName = fileName;

            this.Key = key;
            if (string.IsNullOrEmpty(key))
            {
                this.Key = "media";
            }
            this.Stream = stream;
        }

        public HttpParameter(string objName, Object obj)
        {
            if (string.IsNullOrEmpty(objName)) { throw new ArgumentNullException("objName"); }
            this.Key = objName;
            this.Object = obj;
        }

        public HttpParameter(string key) {
            if (string.IsNullOrEmpty(key)) { throw new ArgumentNullException("key"); }
            this.Key = key;
            this.Value = string.Empty;
        }

        public override string ToString() {
            if (this.IsStream)
            {
                return string.Format("{0}={{Stream}}", this.Key);
            }

            if (this.IsObject)
            {
                return string.Format("{0}={{Object}}", this.Key);
            }

            return string.Format("{0}={1}", this.Key, this.Value);
        }
    }

    public class HttpParameterList : System.Collections.Generic.List<HttpParameter> {
        public HttpParameterList() : base() { }

        public HttpParameterList(object obj) : base() {
            this.Add("obj", obj);

        }


        private bool m_HaveStreamParameter = false;
        private bool m_HaveObjectParameter = false;

        public bool HaveStreamParameter {
            get { return m_HaveStreamParameter; }
        }

        public bool HaveObjectParameter {
            get { return m_HaveObjectParameter; }
        }

        public void SetHaveStreamParameter(bool value) {
            m_HaveStreamParameter = value;
        }
        public void SetHaveObjectParameter(bool value)
        {
            m_HaveObjectParameter = value;
        }

        public void Add(string key, string value) {
            this.Add(new HttpParameter(key, value));
        }

        public void Add(string key, string fileName, System.IO.Stream stream) {
            if (stream == null) {
                throw new ArgumentNullException("stream");
            }
            this.Add(new HttpParameter(key, fileName, stream));
            m_HaveStreamParameter = true;
        }

        public void Add(string key, Object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }
            base.Add(new HttpParameter(key, obj));
            this.m_HaveObjectParameter = true;
        }

        public override string ToString() {
            var sb = new StringBuilder();
            for (int i = 0; i < this.Count; i++) {
                sb.Append(this[i].ToString());
                if (i + 1 != this.Count) {
                    sb.Append("&");
                }
            }

            return sb.ToString();
        }
    }

    public class HttpHeaderList : System.Collections.Generic.List<HttpParameter> {
        public HttpHeaderList() : base() { }

        public void Add(string key, string value) {
            this.Add(new HttpParameter(key, value));
        }

        public void Add(string key, long value)
        {
            this.Add(new HttpParameter(key, value.ToString()));
        }
    }

    public class HttpResponseHeaderList : System.Collections.Generic.List<HttpParameter> {
        public HttpResponseHeaderList() : base() { }

        public void Add(string key, string value) {
            this.Add(new HttpParameter(key, value));
        }

        public void Add(string key, object value) {
            this.Add(new HttpParameter(key, value.ToString()));
        }

        public void AppendHeaders(HttpResponseHeaderList headers) {
            if (headers != null) {
                this.AddRange(headers);
            }
        }
    }
}
